package com.cskaoyan.controller;

import com.cskaoyan.bean.Admin;
import com.cskaoyan.bo.AdminCreateBo;
import com.cskaoyan.bo.AdminDeleteBo;
import com.cskaoyan.bo.AdminUpdateBo;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.vo.AdminVo;
import com.cskaoyan.vo.BaseRespVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * 功能:管理员页面
     * 作者:whj
     */
    @RequestMapping("admin/admin/list")
    public BaseRespVo list(Integer page,Integer limit,String username,String sort,String order){
        AdminVo adminVo = adminService.query(page,limit,username,sort,order);
        return BaseRespVo.ok(adminVo);
    }

    /**
     * 功能:管理员页面,/admin/create
     * 作者:whj
     */
    @RequestMapping("admin/admin/create")
    public BaseRespVo create(@RequestBody AdminCreateBo adminCreateBo){
        Admin admin = adminService.create(adminCreateBo);
        if(admin == null){
            return BaseRespVo.error("用户重复!");
        }
        return BaseRespVo.ok(admin);
    }

    /**
     * 功能:管理员页面,/admin/update
     * 作者:whj
     */
    @RequestMapping("admin/admin/update")
    public BaseRespVo update(@RequestBody AdminUpdateBo adminUpdateBo){
        Admin admin = adminService.update(adminUpdateBo);
        return BaseRespVo.ok(admin);
    }

    /**
     * 功能:管理员页面,/admin/delete
     * 作者:whj
     */
    @RequestMapping("admin/admin/delete")
    @RequiresPermissions(value = "admin:admin:delete")
    public BaseRespVo delete(@RequestBody AdminDeleteBo adminDeleteBo){
        adminService.delete(adminDeleteBo);
        return BaseRespVo.ok();
    }
}
